from sys import argv, stdout as out
from os import system


word_list = {
    'def':'void',
    '~':';',
    '全民制作人们大家好':'printf',
    '（':'(',
    '）':')',
    'music':'int main(){',
    '期待的话请多多为我投票哦':'}',
    '鸡你太美':'{',
    'if_u_need_me':'if',
    '#':'[]',
    'string':'char',
    'import':'#include'
}


def compile(c_file_name):
    exe_file = f'{c_file_name.split(".")[0]}.exe'

    system(f'gcc {c_file_name} -o {exe_file}')

    system(f'{exe_file}')



def split_tokens(exp, c_code, current_token, single_words):

    for char in exp:
        if char not in single_words:
            current_token += char

        if char in single_words:
            convert_token(current_token, c_code)

            convert_token(char, c_code)

            current_token = ''



def convert_token(token, c_code):

    if token in word_list:
        c_code.write(word_list[token])

    elif token == '"' or token == '“' or token == '”':
        c_code.write('"')

    else:
        c_code.write(token)




def main(c_file_name):

    # Words that can stand alone itself
    single_words = {
        '“', '”', '"', '（', '）','(', ')', ',', '，', '~', '\n', '#', ' '
    }


    with open(
        file=c_file_name,
        mode='w',
        encoding='utf-8') as c_code:

        c_code.write('#include<stdio.h>\n')

        with open(file=argv[-1], mode='r', encoding='utf-8') as src_f:

            for exp in src_f.readlines():
                split_tokens(exp, c_code, '', single_words)


    # Compile the .c file into .exe using gcc



try:
    c_file_name = c_file_name = f'{argv[-1].split(".")[0]}.c' if '.' in argv[-1] else f'{argv[-1]}.c'

    if len(argv) > 1:

        main(c_file_name)

        for i in range(1, len(argv) - 1):
            if argv[i] == '-com':
                compile(c_file_name)


    elif len(argv) == 1:
        out.write("No file detected...\nUsage: cxk [FILE_PATH]")



except FileNotFoundError or FileNotFoundError or OSError:
    out.write(f'File [{argv[-1]}] does not exist...')